package com.bjpowernode.boss.mvc.controller;

import com.bjpowernode.boss.mvc.models.AjaxResult;
import com.bjpowernode.boss.mvc.models.dto.operator.LoginResponseDto;
import com.bjpowernode.boss.mvc.models.form.operator.LoginForm;
import com.bjpowernode.boss.mvc.models.menus.SubMenu;
import com.bjpowernode.boss.mvc.service.OperatorService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yu
 * @date 2024/7/6  21:32
 */

@CrossOrigin
@RestController // 前后端分离的项目，使用RestController
@RequestMapping("/operator") // 把公共的前缀放到类的级别来定义
public class OperatorController {
    @Resource
    private OperatorService operatorService ;

    @PostMapping("/login")
    public AjaxResult operatorLogin(@RequestBody @Validated LoginForm loginForm) { // 因为是JSON数据，所以要加上RequestBody
        // 生成返回给前端的登录数据结构中的data部分【operatorid, showName, jwt】
        LoginResponseDto loginResponseDto = operatorService.operatorLogin(loginForm);
        return AjaxResult.OK(loginResponseDto) ;
    }

    @GetMapping("/menus")
    public AjaxResult getMenus(@RequestHeader("operatorId") Long operatorId) {
        // 调用service获取菜单
        List<SubMenu> subMenuList = operatorService.getMenusByLoginOperator(operatorId) ;

        return AjaxResult.OK(subMenuList) ;
    }
}
